package com.normalClasses.eye;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;

public class logReader implements logHandler{
  
   private String logFileName;//name of the log file
  //Pattern pattern = Pattern.compile("(\\d+-\\d+-\\d+)");
 
  public logReader( String logName ){
    
	  this.logFileName=logName;
 
  }
 
  public String[] getAllLog() throws FileNotFoundException{
    File logFile = new File(this.logFileName);
    
    Scanner scanner= new Scanner(logFile);
    
    scanner = new Scanner(logFile);
    
    //scanner.useDelimiter(pattern);
    String information="";
    //int i=0;
    if( scanner.hasNextLine()==false ){
    return null;
    }
    while(scanner.hasNextLine()) {
      
      //System.out.println("第"+i+"次");
      //System.out.println(scanner.hasNextLine());
    String line=scanner.nextLine();
    
    if(line.length()>11&&line.substring(0, 10).matches("\\d+-\\d+-\\d+")) {
      
        information=information+line+"\n";
      
    }
    //i++;
    }
    return information.split("\n");
  
  }
  /**
   * this is a helper method which transform date to day
   * @param date - the date
   * @return - the days
   */
  public long TransformDateToDay(String date) {
    String[] datee=date.split("-");
    return Integer.parseInt(datee[0].trim())*365+Integer.parseInt(datee[1].trim())*30+Integer.parseInt(datee[2].trim());
  }
  /**
   * this is a helper method which transform a date to second
   * @param date
   * @return
   */
  public long transformTimeToSecond(String time){
    String[] timem = time.split(":");
    int second=0;
    second=Integer.parseInt(timem[0])*3600+Integer.parseInt(timem[1])*60;
    return second;
  }
  @Override
  public String[] allLog() throws FileNotFoundException{
    File logFile=new File(this.logFileName);
    Scanner scanner = new Scanner(logFile);
    ArrayList<String> msg=new ArrayList<String>();
    //scanner.useDelimiter(pattern);
    while(scanner.hasNextLine()){
      msg.add(scanner.nextLine());
    }
    String[] information = (String[])msg.toArray(new String[msg.size()]);
    return information;    
  }

  @Override
public String[] infoLog() throws FileNotFoundException{
    File logFile = new File(this.logFileName);
    Scanner scanner =new Scanner(logFile);
    //scanner.useDelimiter(pattern);
    ArrayList<String> msg=new ArrayList<String>();
    while(scanner.hasNextLine()){
      //String line=scanner.next();
    String line=scanner.nextLine();
    if(line.length()>11&&line.substring(0, 10).matches("\\d+-\\d+-\\d+")) {
      if (this.getLogLevel(line).equals("INFO")) {
           msg.add(line);
      }
      }
     }
    String[] information = (String[])msg.toArray(new String[msg.size()]);
    return information;
  }

  @Override
  public String[] traceLog() throws FileNotFoundException {
    File logFile = new File(this.logFileName);
    Scanner scanner =new Scanner(logFile);
    ArrayList<String> msg=new ArrayList<String>();
    //scanner.useDelimiter(pattern);
    while(scanner.hasNextLine()) {
      //String line=scanner.next();
      String line=scanner.nextLine();
      //System.out.println(line.substring(25,30));
      if(line.length()>11&&line.substring(0, 10).matches("\\d+-\\d+-\\d+")) {
      if (this.getLogLevel(line).equals("TRACE")) {
    	  msg.add(line);
      }
        }}
    String[] information = (String[])msg.toArray(new String[msg.size()]);
    return information;
  }

  @Override
  public String[] warnLog() throws FileNotFoundException {
    File logFile = new File(this.logFileName);
    Scanner scanner =new Scanner(logFile);
    //scanner.useDelimiter(pattern);
    ArrayList<String> msg=new ArrayList<String>();
    while(scanner.hasNextLine()) {
      //String line=scanner.next();
      String line=scanner.nextLine();
      if(line.length()>11&&line.substring(0, 10).matches("\\d+-\\d+-\\d+")) {
      if (this.getLogLevel(line).equals("WARN")) {
    	  msg.add(line);
      }
        }}
    String[] information = (String[])msg.toArray(new String[msg.size()]);
    return information;
  }

  @Override
  public String[] debugLog() throws FileNotFoundException {
    File logFile = new File(this.logFileName);
    Scanner scanner =new Scanner(logFile);
    //scanner.useDelimiter(pattern);
    ArrayList<String> msg=new ArrayList<String>();
    while(scanner.hasNextLine()) {
      //String line=scanner.next();
      String line=scanner.nextLine();
      //System.out.println(line.substring(25,30));
      if(line.length()>11&&line.substring(0, 10).matches("\\d+-\\d+-\\d+")) {
      if (this.getLogLevel(line).equals("DEBUG")) {
    	  msg.add(line);
      }
        }}
    String[] information = (String[])msg.toArray(new String[msg.size()]);
    return information;
  }

  @Override
  public String[] errorLog() throws FileNotFoundException {
    File logFile = new File(this.logFileName);
    Scanner scanner =new Scanner(logFile);
    //scanner.useDelimiter(pattern);
    ArrayList<String> msg=new ArrayList<String>();
    
    while(scanner.hasNextLine()) {
      //String line=scanner.next();
      String line=scanner.nextLine();
      if(line.length()>11&&line.substring(0, 10).matches("\\d+-\\d+-\\d+")) {
      //System.out.println(line.substring(25,30));
      if (this.getLogLevel(line).equals("ERROR")) {
    	  msg.add(line);
      }
        }
      }
    String[] information = (String[])msg.toArray(new String[msg.size()]);
    return information;
  }
  @Override
  public String getLogTime(String logLine){
    String[] components = logLine.split("\\s+");
    return components[1];
  }
  @Override
  public String getLogDate(String logLine){
    String[] components = logLine.split("\\s+");
    return components[0];
  }
  @Override
  public String getLogLevel(String logLine) {
    String[] components = logLine.split("\\s+");
    return components[2];
  }
  @Override
  public String getLogSource(String logLine) {
    String[] components = logLine.split("\\s+");
    return components[5]+" "+components[6];
  }
  @Override
  public String getDetailedInformation(String logLine) {
    String information="";
    String[] components = logLine.split("\\s+");
    for(int i=8;i<components.length;i++) {
      information=information+components[i]+" ";
    }
    return information;
  }

  @Override
  public String[] getSpecificLog(String level) throws FileNotFoundException {
    File logFile = new File(this.logFileName);
    Scanner scanner =new Scanner(logFile);
    //scanner.useDelimiter(pattern);
    ArrayList<String> msg=new ArrayList<String>();
    if(level.equals("ALL")){
      return this.allLog();
    }
    else {
    while(scanner.hasNextLine()) {
      //String line=scanner.next();
      String line=scanner.nextLine();
      if(line.length()>11&&line.substring(0, 10).matches("\\d+-\\d+-\\d+")) {
      if (this.getLogLevel(line).equals(level)) {
    	  msg.add(line);
      }
        }} 
    String[] information = (String[])msg.toArray(new String[msg.size()]);
    return information;
  }
   }

  @Override
  public String[] getSpecificLog(String level, String date) throws FileNotFoundException {
    File logFile = new File(this.logFileName);
    Scanner scanner =new Scanner(logFile);
    //scanner.useDelimiter(pattern);
    ArrayList<String> msg=new ArrayList<String>();
    if(level.equals("ALL")) {
      while (scanner.hasNextLine()){
        String line =scanner.nextLine();
        if(line.length()>11&&line.substring(0, 10).matches("\\d+-\\d+-\\d+")) {
        if(this.getLogDate(line).equals(date)) {
        	msg.add(line);
        }
      }}
      String[] information = (String[])msg.toArray(new String[msg.size()]);
      return information;
    }
    else {
      while(scanner.hasNextLine()) {
        String line=scanner.nextLine();
        if(line.length()>11&&line.substring(0, 10).matches("\\d+-\\d+-\\d+")) {
        if(this.getLogDate(line).equals(date)&&this.getLogLevel(line).equals(level)){
        	msg.add(line);
        }
      }}
      String[] information = (String[])msg.toArray(new String[msg.size()]);
      return information;
    }
   
    
  }

  @Override
  public String[] getSpecificLog(String level, String date, String time) throws FileNotFoundException {
  File logFile = new File(this.logFileName);
  Scanner scanner =new Scanner(logFile);
  //scanner.useDelimiter(pattern);
  ArrayList<String> msg=new ArrayList<String>();
  long time1=this.transformTimeToSecond(time);
  if(level.equals("ALL")) {
    while (scanner.hasNextLine()){
      String line =scanner.nextLine();
      if(line.length()>11&&line.substring(0, 10).matches("\\d+-\\d+-\\d+")){
      long time2=this.transformTimeToSecond(this.getLogTime(line));
      if(this.getLogDate(line).equals(date)&&(time1==time2)) {
    	  msg.add(line);
      }
      }
    }
    String[] information = (String[])msg.toArray(new String[msg.size()]);
    return information;
  }
  else {
    while(scanner.hasNextLine()) {
      String line=scanner.nextLine();
      if(line.length()>11&&line.substring(0, 10).matches("\\d+-\\d+-\\d+")){
      long time2 =this.transformTimeToSecond(this.getLogTime(line));
      if(this.getLogDate(line).equals(date)&&this.getLogLevel(line).equals(level)&&(time1==time2)){
    	  msg.add(line);
      }
    }
      }
    String[] information = (String[])msg.toArray(new String[msg.size()]);
    return information;
  }
  }

  @Override
  public String[] getPeriodLog(String level, String startDate, String endDate) throws FileNotFoundException {
    File logFile = new File(this.logFileName);
    Scanner scanner =new Scanner(logFile);
    //scanner.useDelimiter(pattern);
    ArrayList<String> msg=new ArrayList<String>();
    long date1=this.TransformDateToDay(startDate);
    long date2=this.TransformDateToDay(endDate);
    if(level.equals("ALL")) {
      while (scanner.hasNextLine()){
        String line =scanner.nextLine();
        if(line.length()>11&&line.substring(0, 10).matches("\\d+-\\d+-\\d+")) {
        if((this.TransformDateToDay(this.getLogDate(line))<=date2)&&(this.TransformDateToDay(this.getLogDate(line))>=date1)) {
        	msg.add(line);
        }}
      }
      String[] information = (String[])msg.toArray(new String[msg.size()]);
      return information;
    }
    else {
      while(scanner.hasNextLine()) {
        String line=scanner.nextLine();
        if(line.length()>11&&line.substring(0, 10).matches("\\d+-\\d+-\\d+")) {
        if(this.getLogLevel(line).equals(level)&&(this.TransformDateToDay(this.getLogDate(line))<=date2)&&(this.TransformDateToDay(this.getLogDate(line))>=date1)){
        	msg.add(line);
        }
      }}
      String[] information = (String[])msg.toArray(new String[msg.size()]);
      return information;
    }
  }

  @Override
  public String[] getPeriodLog(String level, String startDate, String endDate, String startTime,String endTime) throws FileNotFoundException {
    File logFile = new File(this.logFileName);
    Scanner scanner =new Scanner(logFile);
    //scanner.useDelimiter(pattern);
    ArrayList<String> msg=new ArrayList<String>();
    long date1=this.TransformDateToDay(startDate);
    long date2=this.TransformDateToDay(endDate);
    long time1=this.transformTimeToSecond(startTime);
    long time2=this.transformTimeToSecond(endTime);
    if(level.equals("ALL")){
      while(scanner.hasNextLine()){
        String line = scanner.nextLine();
        if(line.length()>11&&line.substring(0, 10).matches("\\d+-\\d+-\\d+")){
        if(this.getLogDate(line).equals(startDate)&&date1!=date2){
          if(this.transformTimeToSecond(this.getLogTime(line))>=time1) {
        	  msg.add(line);
          }
          }
       else if(this.getLogDate(line).equals(endDate)&&date1!=date2) {
          if(this.transformTimeToSecond(this.getLogTime(line))<=time2) {
        	  msg.add(line);
          }
          }
       else if((this.TransformDateToDay(this.getLogDate(line))<date2)&&(this.TransformDateToDay(this.getLogDate(line))>date1)) {
        	msg.add(line);
        }
       else if(this.transformTimeToSecond(this.getLogTime(line))>=time1&&this.transformTimeToSecond(this.getLogTime(line))<=time2) {
    	   msg.add(line);
       }
        }
        }
    }
    else {
      while(scanner.hasNextLine()){
        String line=scanner.nextLine();
        if(line.length()>11&&line.substring(0, 10).matches("\\d+-\\d+-\\d+")) {
        if(this.getLogLevel(line).equals(level)) {
        	if(this.getLogDate(line).equals(startDate)&&date1!=date2){
                if(this.transformTimeToSecond(this.getLogTime(line))>=time1) {
              	  msg.add(line);
                }
                }
             else if(this.getLogDate(line).equals(endDate)&&date1!=date2) {
                if(this.transformTimeToSecond(this.getLogTime(line))<=time2) {
              	  msg.add(line);
                }
                }
             else if((this.TransformDateToDay(this.getLogDate(line))<date2)&&(this.TransformDateToDay(this.getLogDate(line))>date1)) {
              	msg.add(line);
              }
             else if(this.transformTimeToSecond(this.getLogTime(line))>=time1&&this.transformTimeToSecond(this.getLogTime(line))<=time2) {
          	   msg.add(line);
             }
          }
        }
      }
      }
    String[] information = (String[])msg.toArray(new String[msg.size()]);
    return information;
    }


}

  

 
  
  
  

